package mapkit.handlers
{
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;

	import mapkit.MapHandler;
	import mapkit.carto.Envelope;

	public class KeyboardDefaults extends MapHandler
	{
		private var _panStep:Number=20;

		public function get panStep():Number
		{
			return _panStep;
		}

		public function set panStep(value:Number):void
		{
			_panStep=value;
		}

		private var _zoomStep:Number=0.8;

		public function get zoomStep():Number
		{
			return _zoomStep;
		}

		public function set zoomStep(value:Number):void
		{
			_zoomStep=value;
		}

		override public function get keyboardLocked():Boolean
		{
			return true;
		}

		override public function get mouseLocked():Boolean
		{
			return false;
		}

		override protected function load():void
		{
			super.load();
			setKeyboardFocus();
			map.addEventListener(Event.ENTER_FRAME, setKeyboardFocus);
			map.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);

			addEventListener(KeyboardDefaultsEvent.UP, upHandler);
			addEventListener(KeyboardDefaultsEvent.DOWN, downHandler);
			addEventListener(KeyboardDefaultsEvent.LEFT, leftHandler);
			addEventListener(KeyboardDefaultsEvent.RIGHT, rightHandler);
			addEventListener(KeyboardDefaultsEvent.ZOOMIN, zoomInHandler);
			addEventListener(KeyboardDefaultsEvent.ZOOMOUT, zoomOutHandler);
		}

		override protected function unload():void
		{
			map.removeEventListener(Event.ENTER_FRAME, setKeyboardFocus);
			map.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);

			removeEventListener(KeyboardDefaultsEvent.UP, upHandler);
			removeEventListener(KeyboardDefaultsEvent.DOWN, downHandler);
			removeEventListener(KeyboardDefaultsEvent.LEFT, leftHandler);
			removeEventListener(KeyboardDefaultsEvent.RIGHT, rightHandler);
			removeEventListener(KeyboardDefaultsEvent.ZOOMIN, zoomInHandler);
			removeEventListener(KeyboardDefaultsEvent.ZOOMOUT, zoomOutHandler);
			super.unload();
		}

		protected function setKeyboardFocus(event:Event=null):void
		{
			map.layers.setFocus();
		}

		protected function keyUpHandler(event:KeyboardEvent):void
		{
			var action:int=0;
			switch (event.keyCode)
			{
				case Keyboard.UP:
					dispatchEvent(new KeyboardDefaultsEvent(KeyboardDefaultsEvent.UP,
						map, event));
					break;
				case Keyboard.DOWN:
					dispatchEvent(new KeyboardDefaultsEvent(KeyboardDefaultsEvent.DOWN,
						map, event));
					break;
				case Keyboard.LEFT:
					dispatchEvent(new KeyboardDefaultsEvent(KeyboardDefaultsEvent.LEFT,
						map, event));
					break;
				case Keyboard.RIGHT:
					dispatchEvent(new KeyboardDefaultsEvent(KeyboardDefaultsEvent.RIGHT,
						map, event));
					break;
			}
			switch (String.fromCharCode(event.charCode))
			{
				case "W":
				case "w":
					dispatchEvent(new KeyboardDefaultsEvent(KeyboardDefaultsEvent.UP,
						map, event));
					break;
				case "S":
				case "s":
					dispatchEvent(new KeyboardDefaultsEvent(KeyboardDefaultsEvent.DOWN,
						map, event));
					break;
				case "A":
				case "a":
					dispatchEvent(new KeyboardDefaultsEvent(KeyboardDefaultsEvent.LEFT,
						map, event));
					break;
				case "D":
				case "d":
					dispatchEvent(new KeyboardDefaultsEvent(KeyboardDefaultsEvent.RIGHT,
						map, event));
					break;
				case "+":
				case "=":
					dispatchEvent(new KeyboardDefaultsEvent(KeyboardDefaultsEvent.ZOOMIN,
						map, event));
					break;
				case "-":
				case "_":
					dispatchEvent(new KeyboardDefaultsEvent(KeyboardDefaultsEvent.ZOOMOUT,
						map, event));
					break;
			}
		}

		protected function upHandler(event:KeyboardDefaultsEvent):void
		{
			var extent:Envelope=map.extent;
			extent.pan(0, panStep);
			map.extent=extent;
		}

		protected function downHandler(event:KeyboardDefaultsEvent):void
		{
			var extent:Envelope=map.extent;
			extent.pan(0, -panStep);
			map.extent=extent;
		}

		protected function leftHandler(event:KeyboardDefaultsEvent):void
		{
			var extent:Envelope=map.extent;
			extent.pan(-panStep, 0);
			map.extent=extent;
		}

		protected function rightHandler(event:KeyboardDefaultsEvent):void
		{
			var extent:Envelope=map.extent;
			extent.pan(panStep, 0);
			map.extent=extent;
		}

		protected function zoomInHandler(event:KeyboardDefaultsEvent):void
		{
			var extent:Envelope=map.extent;
			extent.zoom(zoomStep);
			map.extent=extent;
		}

		protected function zoomOutHandler(event:KeyboardDefaultsEvent):void
		{
			var extent:Envelope=map.extent;
			extent.zoom(1 / zoomStep);
			map.extent=extent;
		}

	}
}